﻿// Decompiled with JetBrains decompiler
// Type: MCM.LightInject.CompositionRootExecutor
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

#nullable disable
namespace MCM.LightInject
{
  [ExcludeFromCodeCoverage]
  internal class CompositionRootExecutor : ICompositionRootExecutor
  {
    private readonly IServiceRegistry serviceRegistry;
    private readonly Func<Type, ICompositionRoot> activator;
    private readonly IList<Type> executedCompositionRoots = (IList<Type>) new List<Type>();
    private readonly object syncRoot = new object();

    public CompositionRootExecutor(
      IServiceRegistry serviceRegistry,
      Func<Type, ICompositionRoot> activator)
    {
      this.serviceRegistry = serviceRegistry;
      this.activator = activator;
    }

    public void Execute(Type compositionRootType)
    {
      if (this.executedCompositionRoots.Contains(compositionRootType))
        return;
      lock (this.syncRoot)
      {
        if (!this.executedCompositionRoots.Contains(compositionRootType))
        {
          this.executedCompositionRoots.Add(compositionRootType);
          this.activator(compositionRootType).Compose(this.serviceRegistry);
        }
      }
    }

    public void Execute<TCompositionRoot>(TCompositionRoot compositionRoot) where TCompositionRoot : ICompositionRoot
    {
      if (this.executedCompositionRoots.Contains(typeof (TCompositionRoot)))
        return;
      lock (this.syncRoot)
      {
        if (!this.executedCompositionRoots.Contains(typeof (TCompositionRoot)))
        {
          this.executedCompositionRoots.Add(typeof (TCompositionRoot));
          compositionRoot.Compose(this.serviceRegistry);
        }
      }
    }
  }
}
